package com.example.androidexample.animation;

import android.annotation.SuppressLint;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.IdRes;

import com.example.androidexample.R;

public class TranslateAnimationBuilder implements AnimationBuilder, SeekBar.OnSeekBarChangeListener, View.OnClickListener {

    private final AnimationActivity activity;
    private final AnimationPivotParamsProvider pivotParamsBuilder;
    private float fromXValue = 0f;
    private float toXValue = 1f;
    private float fromYValue = 0f;
    private float toYValue = 1f;
    private TextView tvTranslateStartX;
    private TextView tvTranslateStartY;
    private TextView tvTranslateEndX;
    private TextView tvTranslateEndY;

    public TranslateAnimationBuilder(AnimationActivity activity, AnimationPivotParamsProvider pivotParamsBuilder) {
        this.activity = activity;
        this.pivotParamsBuilder = pivotParamsBuilder;
        resetXYValue();
        initView();
    }

    private void initView() {
        SeekBar skbTranslateStartX = activity.findViewById(R.id.skb_translate_start_x);
        skbTranslateStartX.setOnSeekBarChangeListener(this);
        tvTranslateStartX = activity.findViewById(R.id.tv_translate_start_x);

        SeekBar skbTranslateStartY = activity.findViewById(R.id.skb_translate_start_y);
        skbTranslateStartY.setOnSeekBarChangeListener(this);
        tvTranslateStartY = activity.findViewById(R.id.tv_translate_start_y);

        SeekBar skbTranslateEndX = activity.findViewById(R.id.skb_translate_end_x);
        skbTranslateEndX.setOnSeekBarChangeListener(this);
        tvTranslateEndX = activity.findViewById(R.id.tv_translate_end_x);

        SeekBar skbTranslateEndY = activity.findViewById(R.id.skb_translate_end_y);
        skbTranslateEndY.setOnSeekBarChangeListener(this);
        tvTranslateEndY = activity.findViewById(R.id.tv_translate_end_y);

        Button btTranslate = activity.findViewById(R.id.bt_translate);
        btTranslate.setOnClickListener(this);
    }

    private void resetXYValue() {
        fromXValue = 0f;
        toXValue = 1f;
        fromYValue = 0f;
        toYValue = 1f;
    }

    @Override
    public Animation buildAnimation() {
        return new TranslateAnimation(pivotParamsBuilder.getPivotXType(),fromXValue,pivotParamsBuilder.getPivotXType(),toXValue,
                pivotParamsBuilder.getPivotYType(),fromYValue,pivotParamsBuilder.getPivotYType(),toYValue);
    }

    @SuppressLint({"SetTextI18n", "NonConstantResourceId"})
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        @IdRes
        int seekBarId = seekBar.getId();
        switch (seekBarId) {
            case R.id.skb_translate_start_x:{
                fromXValue = progress/100.0f;
                tvTranslateStartX.setText(fromXValue+"");
            } break;
            case R.id.skb_translate_start_y:{
                fromYValue = progress/100.0f;
                tvTranslateStartY.setText(fromYValue+"");
            } break;
            case R.id.skb_translate_end_x:{
                toXValue = progress/100.0f;
                tvTranslateEndX.setText(toXValue+"");
            } break;
            case R.id.skb_translate_end_y:{
                toYValue = progress/100.0f;
                tvTranslateEndY.setText(toYValue+"");
            } break;
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) { }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) { }

    @Override
    public void onClick(View v) {
        activity.handleAction(AnimationBuilderType.TRANSLATE, this);
    }
}
